ga.core.individual.population
Class KMeansClusterPopulation<T extends IClusterableIndividual<T>>

java.lang.Object
  extended by ga.core.individual.population.KMeansClusterPopulation<T>
Type Parameters:
T - The generic type of individuals.
All Implemented Interfaces:
IClusterPopulation<T>, IPopulation<T>, java.lang.Iterable<T>

public class KMeansClusterPopulation<T extends IClusterableIndividual<T>>
extends java.lang.Object
implements IClusterPopulation<T>

Implementation of a clusterable population that uses the KMeansPlusPlusClusterer of Apache Commons Math.

Since:
11.08.2012
Author:
Stephan Dreyer

Field Summary
private  boolean allowDuplicates
           
private  int clusterCount
           
private  org.apache.commons.math.stat.clustering.KMeansPlusPlusClusterer<T> clusterer
           
private  java.util.List<org.apache.commons.math.stat.clustering.Cluster<T>> clusters
           
private  IFitnessEvaluator<T> evaluator
           
private  IIndividualFactory<T> factory
           
private  int initIndividualCount
           
private static java.util.logging.Logger LOGGER
           
private static int MAX_ITERATIONS
           
private  IndividualList<T> pop
           
private  java.util.Random rnd
           
 
Constructor Summary
KMeansClusterPopulation(IIndividualFactory<T> factory, int initIndividualCount, int clusterCount)
          Creates a new clusterable population.
KMeansClusterPopulation(IIndividualFactory<T> factory, int initIndividualCount, int clusterCount, boolean allowDuplicates)
          Creates a new clusterable population.
 
Method Summary
 void addIndividual(T individual)
          Adds an individual to the population.
 void addIndividuals(IndividualList<T> individuals)
          Adds individuals to the population.
 void addIndividuals(T... individuals)
          Adds individuals to the population.
 void assignFitness()
          Assign fitness in all clusters.
 void assignFitness(T ind)
          Searches for the cluster containing the given individual and assigns fitness to all individuals in the cluster.
 void clear()
          Clears the population.
 boolean containsAny(IndividualList<T> list)
          Determine if one of the individuals is contained in this list.
 void doClustering()
          Divides the population into clusters.
 void evaluateAutomatic()
          Evaluates all individiduals using the automatic evaluator.
 T getEliteIndividual()
          Gets the elite individual, if elite strategy is used.
 int getEvaluatedIndividualCount()
          Get the number of individuals that has been evaluated.
 T getFittestIndividual()
          Gets the individual if the highest fitness.
 IndividualList<T> getIndividuals()
          Getter for a list representation of the population.
 int getInitIndividualCount()
          Get the number of initial individuals.
 T getRandomIndividualForEvaluation()
          Randomly selects an individual to evaluate.
 T getRandomIndividualForSelection()
          Randomly selects an individual to select.
 IndividualList<T> getUnevaluatedIndividuals()
          Get all individuals that has not been evaluated yet.
 T getUnfittestIndividual()
          Gets the individual if the lowest fitness.
 void initRandomly(IValidator<T> validator, GAContext context)
          Fill the population with random individuals.
 boolean isAllowDuplicates()
          Determines if duplicates are allowed.
 boolean isEmpty()
          Determines if the population is empty.
 java.util.Iterator<T> iterator()
           
 void setEvaluator(IFitnessEvaluator<T> evaluator)
          Sets the evaluator.
 void setInitIndividualCount(int individualCount)
          Set the number of initial individuals.
 int size()
          Returns the size of the population; the count of individuals.
 java.lang.String toClusterString()
          Method that creates a string representation of the population, usually for debugging purposes.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER

MAX_ITERATIONS

private static final int MAX_ITERATIONS
See Also:
Constant Field Values

pop

private final IndividualList<T extends IClusterableIndividual<T>> pop

factory

private final IIndividualFactory<T extends IClusterableIndividual<T>> factory

evaluator

private IFitnessEvaluator<T extends IClusterableIndividual<T>> evaluator

allowDuplicates

private boolean allowDuplicates

rnd

private final java.util.Random rnd

initIndividualCount

private int initIndividualCount

clusterCount

private int clusterCount

clusters

private java.util.List<org.apache.commons.math.stat.clustering.Cluster<T extends IClusterableIndividual<T>>> clusters

clusterer

private final org.apache.commons.math.stat.clustering.KMeansPlusPlusClusterer<T extends IClusterableIndividual<T>> clusterer
Constructor Detail

KMeansClusterPopulation

public KMeansClusterPopulation(IIndividualFactory<T> factory,
                               int initIndividualCount,
                               int clusterCount)
Creates a new clusterable population.

Parameters:
factory - The individual factory.
initIndividualCount - Init size of the population.
clusterCount - Number of clusters to divide the population into.
Since:
11.08.2012

KMeansClusterPopulation

public KMeansClusterPopulation(IIndividualFactory<T> factory,
                               int initIndividualCount,
                               int clusterCount,
                               boolean allowDuplicates)
Creates a new clusterable population.

Parameters:
factory - The individual factory.
initIndividualCount - Init size of the population.
clusterCount - Number of clusters to divide the population into.
allowDuplicates - Allow duplicate individuals if true but prevent them otherwise.
Since:
11.08.2012
Method Detail

setEvaluator

public void setEvaluator(IFitnessEvaluator<T> evaluator)
Description copied from interface: IPopulation
Sets the evaluator.

Specified by:
setEvaluator in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
evaluator - The evaluator.

initRandomly

public void initRandomly(IValidator<T> validator,
                         GAContext context)
Description copied from interface: IPopulation
Fill the population with random individuals.

Specified by:
initRandomly in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
validator - The validator or null.
context - The GA context.

getIndividuals

public IndividualList<T> getIndividuals()
Description copied from interface: IPopulation
Getter for a list representation of the population.

Specified by:
getIndividuals in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
The individuals as list.

addIndividuals

public void addIndividuals(T... individuals)
Description copied from interface: IPopulation
Adds individuals to the population.

Specified by:
addIndividuals in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
individuals - Individuals to add.

addIndividual

public void addIndividual(T individual)
Description copied from interface: IPopulation
Adds an individual to the population.

Specified by:
addIndividual in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
individual - Individual to add.

addIndividuals

public void addIndividuals(IndividualList<T> individuals)
Description copied from interface: IPopulation
Adds individuals to the population.

Specified by:
addIndividuals in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
individuals - Individuals to add.

clear

public void clear()
Description copied from interface: IPopulation
Clears the population.

Specified by:
clear in interface IPopulation<T extends IClusterableIndividual<T>>

evaluateAutomatic

public void evaluateAutomatic()
Description copied from interface: IPopulation
Evaluates all individiduals using the automatic evaluator.

Specified by:
evaluateAutomatic in interface IPopulation<T extends IClusterableIndividual<T>>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

size

public int size()
Description copied from interface: IPopulation
Returns the size of the population; the count of individuals.

Specified by:
size in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
Size.

getUnfittestIndividual

public T getUnfittestIndividual()
Description copied from interface: IPopulation
Gets the individual if the lowest fitness.

Specified by:
getUnfittestIndividual in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
The unfittest individual.

getFittestIndividual

public T getFittestIndividual()
Description copied from interface: IPopulation
Gets the individual if the highest fitness.

Specified by:
getFittestIndividual in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
The fittest individual.

getEliteIndividual

public T getEliteIndividual()
Description copied from interface: IPopulation
Gets the elite individual, if elite strategy is used.

Specified by:
getEliteIndividual in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
The elite or null.

getRandomIndividualForEvaluation

public T getRandomIndividualForEvaluation()
Description copied from interface: IPopulation
Randomly selects an individual to evaluate.

Specified by:
getRandomIndividualForEvaluation in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
Random individual to evaluate.

getRandomIndividualForSelection

public T getRandomIndividualForSelection()
Description copied from interface: IPopulation
Randomly selects an individual to select.

Specified by:
getRandomIndividualForSelection in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
Random individual to select.

getUnevaluatedIndividuals

public IndividualList<T> getUnevaluatedIndividuals()
Description copied from interface: IPopulation
Get all individuals that has not been evaluated yet.

Specified by:
getUnevaluatedIndividuals in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
List of unevaluated individuals.

toClusterString

public java.lang.String toClusterString()
Method that creates a string representation of the population, usually for debugging purposes.

Returns:
The population as string.
Since:
11.08.2012

isEmpty

public boolean isEmpty()
Description copied from interface: IPopulation
Determines if the population is empty.

Specified by:
isEmpty in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
true if the population is empty.

isAllowDuplicates

public boolean isAllowDuplicates()
Description copied from interface: IPopulation
Determines if duplicates are allowed.

Specified by:
isAllowDuplicates in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
true if duplicates are allowed.

getEvaluatedIndividualCount

public int getEvaluatedIndividualCount()
Description copied from interface: IPopulation
Get the number of individuals that has been evaluated.

Specified by:
getEvaluatedIndividualCount in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
Number of evaluated individuals.

containsAny

public boolean containsAny(IndividualList<T> list)
Description copied from interface: IPopulation
Determine if one of the individuals is contained in this list.

Specified by:
containsAny in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
list - Individuals to look for.
Returns:
true if this list contains any individual of the other one.

iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface IPopulation<T extends IClusterableIndividual<T>>
Specified by:
iterator in interface java.lang.Iterable<T extends IClusterableIndividual<T>>

getInitIndividualCount

public int getInitIndividualCount()
Description copied from interface: IPopulation
Get the number of initial individuals.

Specified by:
getInitIndividualCount in interface IPopulation<T extends IClusterableIndividual<T>>
Returns:
Number of init individuals.

setInitIndividualCount

public void setInitIndividualCount(int individualCount)
Description copied from interface: IPopulation
Set the number of initial individuals.

Specified by:
setInitIndividualCount in interface IPopulation<T extends IClusterableIndividual<T>>
Parameters:
individualCount - Number of init individuals.

assignFitness

public void assignFitness(T ind)
Description copied from interface: IClusterPopulation
Searches for the cluster containing the given individual and assigns fitness to all individuals in the cluster.

Specified by:
assignFitness in interface IClusterPopulation<T extends IClusterableIndividual<T>>
Parameters:
ind - Individual that has fitness.

assignFitness

public void assignFitness()
Description copied from interface: IClusterPopulation
Assign fitness in all clusters. Requires the centers of the clusters to have fitness.

Specified by:
assignFitness in interface IClusterPopulation<T extends IClusterableIndividual<T>>

doClustering

public void doClustering()
Description copied from interface: IClusterPopulation
Divides the population into clusters.

Specified by:
doClustering in interface IClusterPopulation<T extends IClusterableIndividual<T>>